$import("com.hh.workflow.designer.WorkFlowListTree");
Ext.define('com.hh.workflow.designer.WorkFlowDesigner', {
	extend : 'com.hh.base.BaseServicePanel',
	title : '流程设计',
	layout : 'border',
	form_id : null,
	constructor : function(config) {
		this.config = config || {};
		this.superclass.constructor.call(this, this.config);
		var mainPanel = this.createMainPanel();
		var workFlowListTree = this.createWorkFlowListTree();
		this.add(workFlowListTree);
		this.add(mainPanel);
		this.on("beforerender", function() {
					Doing.hidden(false);
				});
	},
	createWorkFlowListTree : function() {
		var page = this;
		var workFlowListTree = this.workFlowListTree = Ext.create(
				'com.hh.workflow.designer.WorkFlowListTree', {
					getTreePanelListeners : function() {
						return {
							itemclick : function(view, record, item) {
								if (record.get("leaf")) {
									var iframe = window.frames[page.iframeid];
									var object = Request
											.synRequestObject(
													'workflow-WorkFlowInfo-findObjectByDataId',
													{
														dataId : record
																.get("id")
													});
									var graph = iframe.mxDocument.graph;
									iframe.dataId = record.get("id");
									iframe.workflowName = record.get("text");
									if (object != null) {
										iframe.objectId = object.id;
										var mxgraphXml = object.mxgraphXml;
										var xml = iframe.createXml(mxgraphXml);
										var dec = new iframe.mxCodec(xml.documentElement.ownerDocument);
										dec.decode(xml.documentElement, graph
														.getModel());
									} else {
										iframe.objectId = '';
										var xml = iframe
												.createXml('<mxGraphModel><root><Workflow label="'
														+ record.get("text")
														+ '" description="" href="" id="0" hrefParams="" data="{}"><mxCell/></Workflow><Layer label="Default Layer" id="1"><mxCell parent="0"/></Layer></root></mxGraphModel>');
										var dec = new iframe.mxCodec(xml.documentElement.ownerDocument);
										dec.decode(xml.documentElement, graph
														.getModel());
									}
								}
							}
						}
					},
					getExBeTbarItems : function() {
						var panel = this;
						return [{
							icon : 'struts-image?path=com/hh/workflow/img/application_lightning.png',
							text : '部署',
							handler : function() {
								panel.doDeploy();
							}
						}];
					},
					doDeploy : function() {
						var page = this;
						// var record = this.tree.getSelectionModel()
						// .getSelection();
						var record = page.tree.getSelectionModel().lastFocused;
						if (Util.isNull(record)) {
							ExtFrame.info('请选中要部署的流程！');
						} else {
							if (!Util.isNull(record)) {
								Request.synRequestObject(
										'workflow-Activiti-deploy', {
											dataId : record.get("id")
										});
							} else {
								ExtFrame.info('请选中要部署的流程！');
							}
						}
					}
				});
		return workFlowListTree;
	},
	createMainPanel : function() {
		this.iframeid = UUID.getUUID();
		var page = this;
		this.mainPanel = Ext.create('Ext.panel.Panel', {
			padding : '1',
			tools : [{
						type : 'refresh',
						handler : function(c, t, tool) {
							window.frames[page.iframeid].location.reload();
						}
					}],
			icon : 'struts-image?path=com/hh/workflow/img/computer2_16x16.gif',
			autoScroll : true,
			region : 'center',
			title : '设计器',
			html : '<iframe id="'
					+ page.iframeid
					+ '"  name="'
					+ page.iframeid
					+ '"  width=100% height=100%  frameborder=0 src="jsp/workfloweditor.jsp" ></iframe>',
			listeners : {
				beforeDestroy : function(panel) {
					var iframe = document.getElementById(page.iframeid);
					iframe.src = "about:blank";
					iframe.parentElement.removeChild(iframe);
				}
			}
		});
		return this.mainPanel;
	}
});